package com.ply.flashsalessystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.pojo.*;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.status.OrderStatus;
import com.ply.flashsalessystem.service.*;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author XiaoPan
 * date: 2022/5/3 23:28
 * <p>
 * action:
 */
@RestController("/")
@Slf4j
public class PayCon {
    @Autowired
    UserService userService;

    @Autowired
    OrderForGoodsService orderForGoodsService;

    @Autowired
    OrderForGoodsDetaileService orderForGoodsDetaileService;

    @Autowired
    UserAmountService userAmountService;

    @Autowired
    GoodsService goodsService;

//    @AuthorityName(authorityId = 30, authorityName = "用户 为订单付款")
//    @UserAuthority
    @ApiOperation("用户 为订单付款")
    @GetMapping("/payForOrder/{ofgID}")
    @Transactional
    public Result payForOrder(@PathVariable("ofgID") Integer ofgID) {
        log.debug("开始付款");
        if (ofgID <= 0) {
            return Result.error().message("订单号错误");
        }
        String form = userService.payForOrder(ofgID);
        if (form.equals("ok")){
            return Result.ok().message("余额付款成功");
        }else {
            return Result.ok().data("form",form);
        }
    }

    @GetMapping("/return")
    public String returnHtml(){
        return "success";
    }

    @PostMapping("/no")
    @Transactional
    public void no(Integer out_trade_no) {
        log.debug("out_trade_no:{}", out_trade_no);
        int ofgId = out_trade_no;
        OrderForGoods byId = orderForGoodsService.getById(ofgId);
        Long userId = byId.getUserId();

        // 用户 余额设置为 0;
        User byId1 = userService.getById(userId);
        byId1.setBalanceMoney(0);
        // 修改 用户余额 为0
        userService.updateById(byId1);
        log.debug("修改 用户余额 为0");

        // 查询 订单
        OrderForGoods orderForGoods = orderForGoodsService.getById(ofgId);
        orderForGoods.setStatus(OrderStatus.DO_PAY);
        //  付款后 修改订单 状态 增加流水记录
        orderForGoodsService.statusToDoPay(ofgId);
        log.debug("付款后 修改订单 状态");

        //增加流水记录
        UserAmount userAmount = new UserAmount();
        // 修改用户id
        userAmount.setUserId(userId);
        userAmount.setAmount(orderForGoods.getAmountDue());
        userAmountService.save(userAmount);
        log.debug("付款后增加流水记录");

    }
}
